/*
 * Created on 2006-2-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;
import system.base.ProductStorage;
import system.base.Warehouse;

import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.vo.GSManufactureAndProvicerVo;
import com.hitrust.gs.bean.GSDictionaryBean;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSUserMenuInfoBean;
import com.hitrust.gs.business.*;
import com.hitrust.gs.dao.GSDictionaryDAO;

import framework.core.ConstDefine;
import framework.core.DBConnection;
/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSOrderInputCreateAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        LoginContext context=(LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        String[] val=aForm.getRequest().getParameter("val").split(",");
        String[] ids=new String[val.length];
        String wareHouseCode="";
        for (int i = 0; i < val.length; i++) {
            ids[i]=val[i].split("@@")[0];
            wareHouseCode=val[i].split("@@")[1];
        }
        //根据仓库code查询仓库信息
        ArrayList tList=new ArrayList();
        DBConnection con=DBConnection.open(true);
        try {
            Warehouse ware=Warehouse.fetchByCode(con, wareHouseCode);
            aForm.getRequest().setAttribute("ware", ware);
            //根据产品编号查询所选产品及供应商信息
            
            GSManufactureProcessor mpro=new GSManufactureProcessor();
            
            for (int i = 0; i < ids.length; i++) {
               ProductStorage storage=ProductStorage.fetch(con, ids[i]);
               GSManufactureBean mbean= mpro.getManufactureDetailByGuId(storage.getProductId());
               ArrayList provList = mpro.getProviderSimpleByconcentricityid(mbean.getGs_concentricity(),wareHouseCode);
               GSManufactureAndProvicerVo temp=new GSManufactureAndProvicerVo();
               temp.setManufactureBean(mbean);
               temp.setProviderList(provList);
               temp.setProductStorage(storage);
               tList.add(temp);
            }
        } catch (RuntimeException e) {
        }finally{
            if (con!=null) {
                con.close();
            }
        }
        aForm.getRequest().setAttribute("manufacture", tList);
        //取得当前用户功能权限
        GSUniteGwProcessor unitePro=new GSUniteGwProcessor();
        ArrayList menuInfoList=unitePro.getUserMenuInfo(context.getLoginUserId());
        boolean isFirstExist = false;	//初审
        boolean isReviewExist = false;	//复审
        boolean isCommitExist = false;	//提交申购
        boolean isShenWoExist = false;	//申沃零配件采购
        
        for(int i=0;i<menuInfoList.size();i++){
            GSUserMenuInfoBean mbean=(GSUserMenuInfoBean) menuInfoList.get(i);
            if(1102==mbean.getMenu_code()) isFirstExist = true;
            if(1103==mbean.getMenu_code()) isReviewExist = true;
            if(1104==mbean.getMenu_code()) isCommitExist = true;
            if(1109==mbean.getMenu_code()) isShenWoExist = true;
        }
        if(isFirstExist && isReviewExist){
            aForm.getRequest().setAttribute("AUDITNEED","true");
        }else{
            aForm.getRequest().setAttribute("AUDITNEED","false");
        }
        //有直接提交申购功能
        if (isCommitExist)
        	aForm.getRequest().setAttribute("isCommitExist", "true");
        else
        	aForm.getRequest().setAttribute("isCommitExist", "false");        		
        //申沃零配件采购
        if (isShenWoExist)
        	aForm.getRequest().setAttribute("isShenWoExist", "true");
        else
        	aForm.getRequest().setAttribute("isShenWoExist", "false");
        
        ArrayList bcList = GSBaseManager.getAllBalanceCategory();
        aForm.getRequest().setAttribute("balancecategory",bcList);
        
        //配送方式
        GSDictionaryDAO dictionary = new GSDictionaryDAO();
        ArrayList delivermodeList = dictionary.queryByParentId(GSDictionaryBean.DeliverMode);
        aForm.getRequest().setAttribute("deliverModeList", delivermodeList);
        
        //结算方式
        ArrayList balanceModeList = dictionary.queryByParentId(GSDictionaryBean.BalanceMode);
        aForm.getRequest().setAttribute("balanceModeList", balanceModeList);
        
        //获取所选仓库送货地址
        ArrayList wareList=unitePro.getWareHouseLevelName(wareHouseCode, wareHouseCode);
        aForm.getRequest().setAttribute("orderdept",wareList);
        return GSBaseConstants.FORWARD_SUCCESS;
    }
}
